3.1 视图函数中的request
Django视图层功能
•处理对应URL的请求,并返回响应数据
•这个请求-响应的过程中可能涉及到两个部份
1、和模型层打交道,访问数据库数据,把数据响应到浏览器
2、和模板层打交道,视图层将基某些数据传递给html模板文件,渲染后把内容响应给浏览器。
视图函数层中的request
•视图函数中的request参数非常重要,
• 在视图函数中,我们可以通过request参数获取和请求有关的所有信息。
如通过type查看request参数的类型
属性/方法 | 描述 |
---|---|
request.path | 请求的路径部份,不包括域名,查询参数和其他组件 |
request.path_into | 请求的完整路径,包括域名、查询参数和其他组件 |
request.get_full_path() | 返回完整的请求路径URI |
request.build_absolute_usri() | 返回完整的请求路径,包括路径,查询参数和其他组件 |
request.method | 请求的HTTP方法,如GET、POST等 |
request.scheme | 请求协议或者HTTPS |
request.GET | 包含所有的GET请求参数,以字典形式返回 |
request.POST | 包含所有的POST请求参数,以字典形式返回 |
request.FILES | 包含所有的上传文件,以字典形式返回 |
request.body | 请求休原始数据 |
request.session | 对象,表示当前会话的状态和数据,可以使用该对象读取或写入会话数据 |
request.COOKIES | 包含所有的cookie,以字典形式返回 |
request.user | 表示当前经过身份验证的用户对象 |
request.is_ajax() | 检查请求是否是AJAX请求,返回布尔值 |
request.is_secure() | 检查请求是否通过HTTPS安全链接进行 |
1、在shn主项目新建导入一个新的app03项目
from django.urls import path,re_path,include
from django.contrib import admin
from django.shortcuts import HttpResponse
def index(requese):
return HttpResponse( "index page 根首页" )
urlpatterns = [
path( 'admin/' , admin.site.urls),
path( 'hello/' , include( "hello.urls" )),
re_path( 'ariticles/' , include( "ariticles.urls" )),
path( "" ,index),
path( "app03/" ,include( "app03.urls" )) #主url路由增加一个项目
]
2、在app03新建一个urls
from django.urls import path
from . import views
urlpatterns = [
path( "test" ,views.test)
]
3、在app03的views增加
from django.shortcuts import HttpResponse
def index(requese):
return HttpResponse( "显示request属性" )
跑起来路径是: 127.0.0.1:8000/app03/test
返回属性:
from django.shortcuts import HttpResponse
from django.core.handlers.wsgi import WSGIRequest
def test(request:WSGIRequest): # :WSGIRequest是类型提示
print(1)
print( "path:" ,request.path)
print( "path_info:" ,request.path_info)
print( "get_full_path:" ,request.get_full_path())
print( "http methon:" , request.method)
print( "scheme:" , request.scheme)
print( "cookis:" , request.COOKIES)
print( "post:" , request.POST)
print( "GET:" , request.GET)
print( "headers:" , request.headers)
return HttpResponse( "显示request属性" )
返回值:
1
path: /app03/test
path_info: /app03/test
get_full_path: /app03/test
http methon: GET
scheme: http
cookis: {'csrftoken': 'IM7UWkrjgIwE7ia7PtfIv7tFvVn7bP5E'}
post: < QueryDict: {} >
GET: < QueryDict: {} >
headers: {'Content-Length': '',
'Content-Type': 'text/plain',
'Host': '127.0.0.1:8000',
'Connection': 'keep-alive',
'Cache-Control': 'max-age=0',
'Sec-Ch-Ua': '"Not_A Brand";v="8", "Chromium";v="120", "Microsoft Edge";v="120"',
'Sec-Ch-Ua-Mobile': '?0',
'Sec-Ch-Ua-Platform': '"Windows"',
'Upgrade-Insecure-Requests' : '1',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0',
'Accept''text/html,application/xhtml+xml,application/xml;q=0.9, image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
'Sec-Fetch-Site': 'none',
'Sc-Fetch-Mode': 'navigate',
'Sec-Fetch-User': '?1',
'Sec-Fetch-Dest': 'document',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9,e n;q=0.8,en-GB;q=0.7,en-US;q=0.6',
'Cookie': 'csrftoken=IM7UWkrjgIwE7ia7PtfIv7tFvVn7bP5E'}
[04/May/2024 23:27:49] "GET /app03/test HTTP/1.1" 200 19